package com.study.animationstudy.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 *
 * @Author： LJH
 * @Time： 2022/12/17
 * @description：
 */
class LayerView(context: Context,attrs:AttributeSet):View(context, attrs) {

    companion object{
        const val TAG = "LayerView"
    }
    //声明上层的画笔对象
    private val mUpPaint = Paint()
    //声明下层的画笔对象
    private val mDownPaint = Paint()
    //声明遮罩的画笔对象
    private val mMaskPaint = Paint()
    //是否只绘制轮廓
    private var onlyLine = true
    //绘图模式
    private lateinit var mMode:PorterDuff.Mode

    init {
        mUpPaint.strokeWidth = 5f
        mUpPaint.color = Color.CYAN
        mDownPaint.strokeWidth = 5f
        mDownPaint.color = Color.RED
    }

    public fun setMode(mode:PorterDuff.Mode){
        mMode = mode
        onlyLine = false
        mUpPaint.style = Paint.Style.FILL
        mDownPaint.style = Paint.Style.FILL
        // 立即刷新视图（线程安全方式）
        postInvalidate()
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        //获取视图的实际宽度
        val width = measuredWidth
        //获取视图的实际高度
        val height = measuredHeight
        // 只绘制轮廓
        if (onlyLine){
            canvas?.apply {
                drawRect((width/3).toFloat(), (height/3).toFloat(),
                    (width*9/10).toFloat(), (height*9/10).toFloat(),mUpPaint)
                drawCircle((width/3).toFloat(), (height/3).toFloat(),
                    (height/3).toFloat(),mDownPaint)
            }
        } else if(mMode != null) {
            // 绘制混合后的图像
            // 创建一个遮罩位图
            val mask = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888)
            // 创建一个遮罩画布
            val canvasMask = Canvas(mask)
            //先绘制上层的矩形
            canvasMask.drawRect(
                (width/3).toFloat(), (height/3).toFloat(),
                (width*9/10).toFloat(), (height*9/10).toFloat(),mUpPaint)
            //设置离屏缓存
            // 设置离屏缓存
            val saveLayer = canvas!!.saveLayer(
                0f,
                0f,
                width.toFloat(),
                height.toFloat(),
                null,
                Canvas.ALL_SAVE_FLAG
            )
            // 再绘制下层的圆形
            // 再绘制下层的圆形
            canvas.drawCircle(
                (width / 3).toFloat(),
                (height / 3).toFloat(),
                (height / 3).toFloat(),
                mDownPaint
            )
            mMaskPaint.xfermode = PorterDuffXfermode(mMode) // 设置混合模式

            canvas.drawBitmap(mask, 0f, 0f, mMaskPaint) // 绘制源图像的遮罩

            mMaskPaint.xfermode = null // 还原混合模式

            canvas.restoreToCount(saveLayer) // 还原画布
        }
    }

    // 只显示线条轮廓
    fun setOnlyLine() {
        onlyLine = true
        mUpPaint.style = Paint.Style.STROKE // 设置画笔的类型
        mDownPaint.style = Paint.Style.STROKE // 设置画笔的类型
        postInvalidate() // 立即刷新视图（线程安全方式）
    }
}